package org.luxor.commons.storage.config;

import org.luxor.commons.core.utils.StrKit;
import org.luxor.commons.core.utils.StringUtils;

/**
 * 防止盗链配置
 *
 * @author Mr.yan @date 2019/12/15
 */
public class HttpConfig {

    final String DEFAULT_CHARSET = "UTF-8";
    final String DEFAULT_SECRE_KEY = "DISK1234567890";
    final String DEFAULT_WEB_SERVER_URL = "http://172.168.1.108:8888";
    final long DEFAULT_TOKEN_TTL = 300;

    /**
     * 开启防盗链Token认证
     */
    private boolean antiStealToken;
    /**
     * 字符集
     */
    private String charset;
    /**
     * Token秘钥
     */
    private String secretKey;
    /**
     * 文件下载地址
     */
    private String webServerUrl;

    /**
     * token TTL (time to live), seconds
     */
    private long ttl;

    public HttpConfig() {
        this.antiStealToken = false;
        this.charset = DEFAULT_CHARSET;
        this.secretKey = DEFAULT_SECRE_KEY;
        this.webServerUrl = DEFAULT_WEB_SERVER_URL;
        this.ttl = DEFAULT_TOKEN_TTL;
    }

    public String getCharset() {
        return charset;
    }

    public void setCharset(String charset) {
        this.charset = charset;
    }

    public Boolean getAntiStealToken() {
        return antiStealToken;
    }

    public void setAntiStealToken(Boolean antiStealToken) {
        this.antiStealToken = antiStealToken;
    }

    public String getSecretKey() {
        return secretKey;
    }

    public void setSecretKey(String secretKey) {
        this.secretKey = secretKey;
    }

    public String getWebServerUrl() {
        return this.webServerUrl;
    }

    public void setWebServerUrl(String webServerUrl) {
        if (StringUtils.endsWith(webServerUrl, StrKit.SLASH)) {
            this.webServerUrl = webServerUrl.substring(0, webServerUrl.length() - 1);
        } else {
            this.webServerUrl = webServerUrl;
        }
    }

    public long getTtl() {
        return ttl;
    }

    public void setTtl(long ttl) {
        this.ttl = ttl;
    }

    /**
     * 获取完整URL地址
     *
     * @param fileFullPath 文件路径
     * @return 完整URL地址
     */
    public String getResAccessUrl(String fileFullPath) {
        if (!StringUtils.startsWith(fileFullPath, StrKit.SLASH)) {
            fileFullPath = StrKit.SLASH + fileFullPath;
        }
        String fileUrl = this.getWebServerUrl() + fileFullPath;
        return fileUrl;
    }

}
